package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * @name 海峡都市在线
 * @link http://www.hdzxw.com/
 * @author dhy
 */ 
public class HdzxwNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.hdzxw.com/[a-z]+/[0-9]+/[0-9]+/[0-9]+.html",
		"http://www.hdzxw.com/[a-z]+/[a-z]+/[0-9]+/[0-9]+/[0-9]+.html",
		"http://www.hdzxw.com/news/[a-z]+/n-[0-9]+.html",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title=doc.select("div.m_Left2 h2").text();
		
		date=DateUtils.matchDate(doc.select("div.info").text());
		
		content =HtmlCleaner.getContentHtml(url, doc.select("div.Con"));

		String authorStr = doc.select("div.info").text();
		if(authorStr.indexOf("|") != -1){
			author = StringUtils.substringBetween(authorStr, "|", "|");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	public static void main(String[] args) {
		
		HdzxwNewsAnalyse analyse=new HdzxwNewsAnalyse();
		String url="http://www.hdzxw.com/news/china/n-5505278825.html";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}

	
